<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件


function is_phone($phone)
{
    return preg_match('/^1[34578]{1}\d{9}$/', $phone);
}

function phone_code($len = 4)
{
    $strs = '012356789';
    $c = [];
    for ($i = 0; $i < $len; $i++) {
        $c[] = $strs[rand(0, 8)];
    }
    return join('', $c);
}


function str_random($length, $is_number = false)
{
    if ($is_number) {
        $strs = '0123456789';
    } else {
        $strs = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
    }
    $max = strlen($strs) - 1;
    $str = '';
    for ($i = 0; $i < $length; $i++) {
        $str .= $strs[rand(0, $max)];
    }
    return $str;
}

function curl($url, $method = 'GET', $data = '')
{
    $ch = curl_init();
    $header = ["Accept-Charset: utf-8"];
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $temp = curl_exec($ch);
    curl_close($ch);
    return $temp;
}

global $redis;
function redis()
{
    global $redis;
    if ($redis)
        return $redis;
    else {
        $redis = new \Redis();
        $redis->pconnect('127.0.0.1', 6378);
        return $redis;
    }
}

function loger($str, $type = 'info')
{
    // echo $str;
    $path = RUNTIME_PATH . 'log_' . $type . '.txt';
    $fd = fopen($path, 'a');
    if (!$fd)
        return;
    fwrite($fd, date('H:i:s '));
    fwrite($fd, $str);
    fwrite($fd, "\r\n");
    fclose($fd);
}

function img_create($path, $ext)
{
    // 1 = GIF，2 = JPG，3 = PNG 6 = BMP
    switch ($ext) {
        case 1:
            return imagecreatefromgif($path);
        case 2:
            return imagecreatefromjpeg($path);
        case  3:
            return imagecreatefrompng($path);
        case 6:
            return imagecreatefromwbmp($path);
        default:
            return null;
    }
}